package com.liqi.service;

import com.liqi.beans.Course;
import com.liqi.beans.Order;
import com.liqi.beans.User;
import com.liqi.feign.StockFeign;
import com.liqi.mapper.OrderMapper;
import com.liqi.util.JwtUtil;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private StockFeign stockFeign;


    @Override
    public User login(User user) {
        return orderMapper.login(user);
    }

    @Override
    public String findById(Integer id) {
        return orderMapper.findById(id);
    }

    @Override
    public List<Course> getCourses() {
        return orderMapper.getCourses();
    }

    @Override
    @Transactional
    public int add(Order order) {


        try{

        }finally {

        }

        Integer total = 0;

        
        order.setTotalPrice(order.getTotalPrice());
        order.setUserId(order.getUserId());
        List<Course> list = order.getCourses();

        int i = orderMapper.addOrder(order);

        if(i > 0){
            for (Course course : list) {
                System.err.println(course);
                stockFeign.decreNum(course.getNum(),course.getId());
            }
            orderMapper.addOrderDetails(list,order.getOrderId());
        }

        return i;
    }


    @Override
    public String createToken(User user){
        return JwtUtil.createJWT(UUID.randomUUID().toString(),user.getId()+"",1000*60l);
    }

}
